/**
 * 我的资料
 * author:zhangjlt
 * date:2016年3月10日10:30:42
 */
;
define(function(require, exports, module) {
	var wechat = require("wechat");
	var myProfile = {
		education: [],
		/**
		 * bindEvent 页面事件
		 */
		bindEvent: function() {
			//返回
			$("#return_myProfile").unbind().on("click", function() {
				window.location.href = "#/index/my";
//window.history.back(-1);
			});

			$(".edit_myprofile").unbind().on("click", function() {
				var locateAttr = $(this).attr("locate");
				window.location.href = "#/index/" + locateAttr;
			});
			$("#birthday_select").unbind().on("click", function() {
				var dtPicker = new mui.DtPicker({
					"type": "date",
					"beginYear": 1949,
					"endYear": 2018
				});
				dtPicker.show(function(selectItems) {

					$("#birthday_year").text(selectItems.y.text);
					$("#birthday_month").text(selectItems.m.text);
					$("#birthday_day").text(selectItems.d.text);
					dtPicker.dispose();
					myProfile.changeUserInfo();
				});
			});
			$("#sex_select").unbind().on("click", function() {
				var picker = new mui.PopPicker();
				picker.setData([{ value: '1', text: '男' }, { value: '2', text: '女' }]);

				picker.show(function(selectItems) {
					$("#sex_myProfile").text(selectItems[0].text);

					myProfile.changeUserInfo();
					picker.dispose();
				}, function(selectItems) {
					picker.dispose();
				});
			});
			$("#education_select").unbind().on("click", function() {
				var picker = new mui.PopPicker();
				picker.setData(myProfile.education);
				picker.show(function(selectItems) {
					$("#education_myProfile").text(selectItems[0].text);
					$("#education_myProfile").attr("educationId", selectItems[0].value);

					myProfile.changeUserInfo();
					picker.dispose();
				}, function(selectItems) {
					picker.dispose();
				});
			});
		},
		/**
		 * 进行页面路由监听配置
		 */
		bindRout: function() {
			//公共页面监听路由
			Path.map("#/index/:page_name").to(function() {
				console.log(this.params["page_name"]);
				var page_name = this.params["page_name"];
				//来自状态
				var from = "right";
				if(page_name == "my") {
					from = "left";
					//记录状态
					common.loadPage({
						pageUrl: "html/" + page_name + ".html",
						reload: true,
						from: from
					});
				} else {
					//记录状态
					common.loadPage({
						pageUrl: "html/" + page_name + ".html",
						reload: true,
						from: from
					});
				}

			});
			//公共页面监听路由
			Path.map("#home").to(function() {
				//返回首页
				common.loadPage({
					pageUrl: "html/index.html",
					reload: false,
					from: "left"
				});
			});
			//Path.root("#home");
			Path.listen();
		},
		/**
		 * 页面数据加载
		 */
		initData: function() {
			var headImgUrl = window.localStorage.getItem("headImgUrl");
			$("#banner_myProfile").attr("src", headImgUrl);
			$("#abbr_myProfile").attr("src", headImgUrl);
			var param = {
				"openId": window.localStorage.getItem("openId"),
				"appId": window.localStorage.getItem("appId"),
			};
			common.loadData({
				url: "/wxmember/service/getUserInfo",
				param: param,
				successCallBack: myProfile.getUserInfoCallBack, //回调函数
				dataSource: "getUserInfo",
				cache: false
			});
		},
		changeUserInfo: function() {

			var param = {
				"openId": window.localStorage.getItem("openId"),
				"appId": window.localStorage.getItem("appId"),
				"birthday": {
					"year": $("#birthday_year").text(),
					"month": $("#birthday_month").text(),
					"day": $("#birthday_day").text()
				},
				"name": $("#name_myProfile").text(),
				"sex": $("#sex_myProfile").text(),
				"education": $("#education_myProfile").text(),
				"educationId": $("#education_myProfile").attr("educationId"),
			};
			common.loadData({
				url: "/wxmember/service/changeUserInfo",
				param: param,
				successCallBack: function() {
					common.showTip({
						content: "修改成功"
					});
				}, //回调函数
				dataSource: "getUserInfo",
				cache: false
			});
		},
		//callback function
		getUserInfoCallBack: function(json) {
			console.log("调用后台ajax返回值为：" + JSON.stringify(json));
			if(json.status == "0") {
				window.localStorage.setItem("userInfoData", JSON.stringify(json.data));
				myProfile.showUserInfo(json);
			} else {
				common.showTip({
					content: json.message
				});
			}
		},
		getEducation: function(array) {
			myProfile.education = [];
			for(var i = 0; i < array.length; i++) {
				var obj = {};
				obj.text = array[i].education;
				obj.value = array[i].educationId;
				myProfile.education.push(obj);
			}
		},
		showUserInfo: function(json) {
			myProfile.getEducation(json.data.educationList);
			$("#name_myProfile").text(json.data.name);
			$("#mobilePhone_myProfile").text(json.data.mobilePhone);
			var birthday = json.data.birthday;
			if(birthday) {
				$("#birthday_year").text(birthday.year ? birthday.year : "");
				$("#birthday_month").text(birthday.month ? birthday.month : "");
				$("#birthday_day").text(birthday.day ? birthday.day : "");
			}
			$("#sex_myProfile").text(json.data.sex ? json.data.sex : "");
			$("#education_myProfile").text(json.data.education ? json.data.education : "");
			$("#education_myProfile").attr("educationId", json.data.educationId ? json.data.educationId : "");
			$("#job_myProfile").text(json.data.job ? json.data.job : "");
			$("#job_myProfile").attr("jobId", json.data.jobId ? json.data.jobId : "")
			var address = json.data.address;
			var str = JSON.stringify(address);
			window.localStorage.setItem("address", str);
			if(address && address.receiverProvince) {
				var addstr = address.receiverProvince + address.receiverCity + address.receiverRegion + address.receiverAddr;
				$("#address_myProfile").text(addstr);
			}
		},
		/**
		 * each page init mothed
		 */
		initialize: function() {
			//动态加载数据
			myProfile.initData();
			//事件绑定
			myProfile.bindEvent();
			//路由绑定
			myProfile.bindRout();
		}
	};

	module.exports = myProfile;
});